/*COPYRIGHT NOTICE: This software is provided by United States Postal Service 
(USPS). It can be used only for ID-tag applications which make use of ID-tags 
in conformity with UPU standard S18c. The software may be freely distributed 
and/or modified.  A condition of such distribution is that this copyright 
notice is always included in the comments heading the software. This software 
is licensed as is without any other warranty of any kind. USPS expressly 
disclaims all warranties or conditions including, but not limited to, implied 
warranties or conditions of merchantability and fitness for particular purpose 
and those arising by statute or otherwise in law or from course of dealing or 
usage of trade. In no event shall USPS be liable for any direct, indirect, 
special, incidental, consequential or other damages arising out of this 
software even if USPS has been advised of the possibility of such damages in 
advance.
*/
/*
*   This module contains the function ninfo_to_bars which generates 
*   an S18c format ID-tag bar code.
*
*   Principal function in this module is:
*   ninfo_to_bars(infon *info, int *bars);
*		Generate bar code with ECC, containing supplied info, being:
*		cbit:		value of the Cbit
*		machine:	machine number in range 0001-3999
*		day:		day of month in range 01-31
*		time:		time of day in half hour increments, range 00-47
*		sequence:	item sequence number, range 00001-24999
*
*	Subsidiary functions are:
*		make_ecc:	calculates ecc bits and parity bit
*		nbits:		sub-function of info_to_bits for single fields
*		info_to_bits:	maps data onto bit values
*		bits_to_bars:	maps bit values and ecc onto bar code
*
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>

/* National ID Tag Data */
typedef struct
    {
    unsigned cbit;		/* Cbit */
    unsigned machine;	/* machine id, range 0001-3999 */
    unsigned day;		/* day of month, range 01-31 */
    unsigned time;		/* 00-47, half hour increments */
    unsigned sequence;	/* mailpiece sequence number, 00001-24999 */
    } infon;

/* function definitions */
void ninfo_to_bars(infon *info, int *bars);

#define GENERATOR 0x1539 /* generator polynomial x12+x10+x8+x5+x4+x3+1 */
#define GENHI     0x1000 /* the x12 term where the data goes */

/*************  DATA VARIABLES ****************/

static int bits[47];           /* data bits only */
static int parbit;             /* the parity bit */
static int this_ecc;           /* ecc bits calculated from bits[] */

/* The following codes[] table lists zero-run-length-limited codes for
 * 4-bar codes 0...9
 * 3-bar codes 0...5
 * 2-bar codes 0...3
 * 1-bar codes 0...1
 *
/*                  bit patterns                               values    */
static unsigned codes[] = {0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,/* 0-6     */
                           0x04,0x07,0x06};                   /* 7-9     */

/*************  FUNCTION DECLARATIONS ****************/

static void make_ecc(void);
static void nbits(int value,  int *field, int length);
static void info_to_bits(infon *info);
static void bits_to_bars(int *bars);

/*************  THE FUNCTIONS ***************/

static void make_ecc(void)
    /* Generate correct this_ecc and parbit matching data in bits[0..46] */
    /* Used by both ninfo_to_bars and correct */
{
int i,j;

    this_ecc = 0;
    parbit = 0;
    for (i=0; i<47; i++)
        {
	    this_ecc <<= 1;				/* left-shift one bit */
	    if (bits[i])
            {
            parbit ^= 1;			/* XOR the parity bit */
	        this_ecc ^= GENHI;		/* XOR the data bit */
            }
	    if (this_ecc & GENHI)		/* replace 12-bit carry+data with generator */
	        this_ecc ^= GENERATOR;	/* binary 010100111001, & turn off GENHI */
	    }

    j = this_ecc;
    for (i=0; i<12; i++)
        {
        parbit ^= (j&1);
        j >>= 1;
        }
}

static void nbits(int value,  int *field, int length)
    /* This function fills the specified number of field bits
     * with the correct bar code for the supplied value.
     * Used by info_to_bits
     */
{
int i;
unsigned bits;

    field += (length-1);
    bits = codes[value];
    for (i=0; i<length; i++)
        {
        *field-- = bits & 1;
        bits >>= 1;
        }
}

static void info_to_bits(infon *info)
    /* Convert information values and to bar code data bits[] */
    /* Used by ninfo_to_bars */
{
int i;

    if (info->cbit)                         /* Cbit */
        bits[0] = 0;
    else
        bits[0] = 1;

    nbits(info->machine/1000, &bits[1],  2); /* machine id M3 */
    i = info->machine % 1000;
    nbits(i/100,              &bits[3],  4); /*            M2 */
    i %= 100;
    nbits(i/10,               &bits[7],  4); /*            M1 */
    nbits(i%10,               &bits[11], 4); /*            M0 */

    nbits(info->day/10,       &bits[15], 3); /* date       D1 */
    nbits(info->day%10,       &bits[18], 4); /*            D0 */

    nbits(info->time/10,      &bits[22], 3); /* time       T1 */
    nbits(info->time%10,      &bits[25], 4); /*            T0 */

    nbits(info->sequence/10000,&bits[29],2); /* sequence   S4 */
    i = info->sequence%10000;
    nbits(i/1000,             &bits[31], 4); /*            S3 */
    i %= 1000;
    nbits(i/100,              &bits[35], 4); /*            S2 */
    i %= 100;
    nbits(i/10,               &bits[39], 4); /*            S1 */
    nbits(i%10,               &bits[43], 4); /*            S0 */
}

static void bits_to_bars(int *bars)
    /* re-order bits[] to bars[] */
    /* Used by ninfo_to_bars */
{
int i;

    bars[0] = 1;                 /* start */
    for (i=0; i<3; i++)
        bars[i+1] = bits[i];     /* CM3 */  /* offset = start bit */
    for (i=3; i<7; i++)
        bars[i+2] = bits[i];     /* M2 */   /* offset = start + E11 */
    for (i=7; i<11; i++)
        bars[i+3] = bits[i];     /* M1 */
    for (i=11; i<15; i++)
        bars[i+4] = bits[i];     /* M0 */
    for (i=15; i<18; i++)
        bars[i+5] = bits[i];     /* D1 */
    for (i=18; i<22; i++)
        bars[i+6] = bits[i];     /* D0 */
    for (i=22; i<25; i++)
        bars[i+7] = bits[i];     /* T1 */
    for (i=25; i<29; i++)
        bars[i+8] = bits[i];     /* T0 */
    for (i=29; i<31; i++)
        bars[i+9] = bits[i];     /* S4 */
    for (i=31; i<35; i++)
        bars[i+10] = bits[i];    /* S3 */
    for (i=35; i<39; i++)
        bars[i+11] = bits[i];    /* S2 */
    for (i=39; i<43; i++)
        bars[i+12] = bits[i];    /* S1 */
    for (i=43; i<47; i++)
        bars[i+13] = bits[i];    /* S0 */   /* offset = start + E11..E0 */

    i = this_ecc;
    bars[60] = parbit;           /* Ep */
    bars[55] = i&1;              /* E0 */
    i>>=1;
    bars[50] = i&1;              /* E1 */
    i>>=1;
    bars[45] = i&1;              /* E2 */
    i>>=1;
    bars[40] = i&1;              /* E3 */
    i>>=1;
    bars[37] = i&1;              /* E4 */
    i>>=1;
    bars[32] = i&1;              /* E5 */
    i>>=1;
    bars[28] = i&1;              /* E6 */
    i>>=1;
    bars[23] = i&1;              /* E7 */
    i>>=1;
    bars[19] = i&1;              /* E8 */
    i>>=1;
    bars[14] = i&1;              /* E9 */
    i>>=1;
    bars[ 9] = i&1;              /* E10 */
    i>>=1;
    bars[ 4] = i&1;              /* E11 */

    bars[61] = 1;                /* stop */
}

void ninfo_to_bars(infon *info, int *bars)
    /* Encode national ID Tag bar code including BCH binary ECC */
{
    info_to_bits(info);       /* convert info to data bits */
    make_ecc();               /* compute this_ecc */
    bits_to_bars(bars);       /* mix bits into bar code */
}
